var amqCon = require('../util/AMQConnection');
var amqCh = require('../util/AMQChannel');
var log4js = require('log4js');
var logger = log4js.getLogger('PlayerManager');

function PlayerManager(config) {
    logger.trace("enter constructor");
    this.config = config;
    this.players = [];
    this.playerClass = require(config.playerClass);
}

module.exports = PlayerManager;

PlayerManager.PlayerManager = PlayerManager;

PlayerManager.prototype.init = function (cb) {
    logger.trace('enter init');

    var that = this;
    this.amqCon = amqCon.getInstance();
    that.chReceive = new amqCh.AMQChannel(that.amqCon);
    that.chReceive.createExchange(that.config.common.playerManager.exchange, that.config.common.playerManager.key, function (obj2) {
        if (obj2 === true) {
            logger.debug('exchange ok, bind queue to this exchange');
            that.chReceive.bindQueue(that.config.common.playerManager.queue, {exclusive: false}, function (msg) {
                that.processData(JSON.parse(msg.content.toString()));
            }, true, function (obj3) {
                if (obj3 === true) {
                    logger.info('bind queue ok, waiting for messages');
                } else {
                    logger.error('cannot bind queue');
                    logger.error(obj3);
                    cb(obj3);
                }
            });
        } else {
            logger.error('cannot create exchange');
            logger.error(obj2);
            cb(obj2);
        }
    });
};

PlayerManager.prototype.processData = function (msg) {
    logger.trace('enter processData');
    logger.debug(msg);
    var i, pl;
    var fnResult = function (result) {
        if (result === true) {
            logger.info('init ok');
        } else {
            logger.info('init not ok');
        }
    };

    switch (msg.action) {
        case 'find_player':
            logger.info('table: ' + msg.table + ', request: ' + msg.total + ' players, key: ' + msg.tableKey + ', from: ' + msg.from);
            for (i = 0; i < msg.total; i += 1) {
                pl = new this.playerClass.Player(msg.from + i + 1, this.config.common);
                this.players.push(pl);
                logger.info('player: ' + pl.name);
                pl.initWithAI(msg.tableKey, fnResult);
            }
            for (i = 0; i < this.players.length; i += 1) {
                logger.info('player: ' + this.players[i].name);
            }
            break;
        case 'delete_player':
            logger.info('delete player with id: ' + msg.playerID);
            this.deletePlayer(msg.playerID);
            break;
        default:
            break;
    }
};

PlayerManager.prototype.deletePlayer = function (id) {
    logger.trace('enter deletePlayer');
    var i;

    for (i = 0; i < this.players.length; i += 1) {
        if (this.players[i].id === id) {
            logger.info('delete player id ' + id + ', at: ' + i);
            logger.debug(this.players[i]);
            this.players.splice(i, 1);
            break;
        }
    }
};
